import { AESEncryption, AESDecryption } from "@/utils/crypto";
let secret = "dyt5e76587ftjkjh";

let encryptLocalSetItem = function (key: string, value: any) {
  let encryptKey = key; //AESEncryption(secret, key);
  let encryptValue = AESEncryption(secret, value);
  return Reflect.set(_encryptLocalStorage, encryptKey, encryptValue);
};

let encryptLocalGetItem = function (key: string) {
  let encryptKey = key; //AESEncryption(secret, key);
  let value = Reflect.get(_encryptLocalStorage, encryptKey);
  if (value) {
    return AESDecryption(secret, value);
  }
  return value;
};

let encryptLocalRemoveItem = function (key: string) {
  let encryptKey = key; //AESEncryption(secret, key);
  return Reflect.deleteProperty(_encryptLocalStorage, encryptKey);
};
let encryptLocalKey = function (index: number) {
  let keys = Object.keys(localStorage);
  return keys[index]; //AESDecryption(secret, keys[index]);
};

let encryptSessionSetItem = function (key: string, value: any) {
  let encryptKey = key; //AESEncryption(secret, key);
  let encryptValue = AESEncryption(secret, value);
  return Reflect.set(_encryptSessionStorage, encryptKey, encryptValue);
};

let encryptSessionGetItem = function (key: string) {
  let encryptKey = key; //AESEncryption(secret, key);
  let value = Reflect.get(_encryptSessionStorage, encryptKey);
  if (value) {
    return AESDecryption(secret, value);
  }
  return value;
};

let encryptSessionRemoveItem = function (key: string) {
  let encryptKey = key; //AESEncryption(secret, key);
  return Reflect.deleteProperty(_encryptSessionStorage, encryptKey);
};

let encryptSessionKey = function (index: number) {
  let keys = Object.keys(sessionStorage);
  return keys[index]; //AESDecryption(secret, keys[index]);
};

let _encryptLocalStorage = new Proxy(window.localStorage, {
  get(target, propKey, receiver) {
    if (propKey === "setItem") return encryptLocalSetItem;
    if (propKey === "getItem") return encryptLocalGetItem;
    if (propKey === "removeItem") return encryptLocalRemoveItem;
    if (propKey === "clear") {
      return target.clear.bind(target);
    }
    if (propKey === "length") return target.length;
    if (propKey === "key") return encryptLocalKey;
    return target[propKey] || "";
  },
  set(obj, prop, value) {
    return Reflect.set(obj, prop, value);
  },
});

let _encryptSessionStorage = new Proxy(window.sessionStorage, {
  get(target, propKey, receiver) {
    if (propKey === "setItem") return encryptSessionSetItem;
    if (propKey === "getItem") return encryptSessionGetItem;
    if (propKey === "removeItem") return encryptSessionRemoveItem;
    if (propKey === "clear") {
      return target.clear.bind(target);
    }
    if (propKey === "length") return target.length;
    if (propKey === "key") return encryptSessionKey;
    return target[propKey] || "";
  },
  set(obj, prop, value) {
    return Reflect.set(obj, prop, value);
  },
});

Object.defineProperty(window, "localStorage", {
  configurable: true,
  enumerable: true,
  value: _encryptLocalStorage,
});

Object.defineProperty(window, "sessionStorage", {
  configurable: true,
  enumerable: true,
  value: _encryptSessionStorage,
});
